#include<sys/socket.h>
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<string>
#include<fstream>
#include<arpa/inet.h>
using namespace std;

char recstr[1024];

struct cliaddr{
        sockaddr_in cliad;
        cliaddr * next;
        char flag;
};

int main(){
    int socs,adt;
    string reqw,reql,reqa,recvs,reqc;
    reqw="waiting";
    reql="opering";
    reqa="losting";
    reqc="cancing";
    cliaddr * head=new cliaddr;
    cliaddr * now;
    cliaddr * now1;
    cliaddr * now2;
    head->next=0;
    head->flag=0;
    now=head;
    now1=now;
    head->cliad.sin_addr.s_addr=0;
    char * addrins=NULL;
    fstream logfile;
    logfile.open("serversoc.log",ios_base::out|ios_base::app);
    if((socs=socket(AF_INET ,SOCK_DGRAM ,IPPROTO_UDP))<0){
        cout<<"创建socket失败。"<<endl;
    }
    else{
        sockaddr_in serad,adin;
        serad.sin_family = AF_INET;
        serad.sin_addr.s_addr = htonl(INADDR_ANY); 
        serad.sin_port = htons(10240); 
        if(bind(socs, (struct sockaddr*)&serad, sizeof(serad))==-1){
            perror("绑定失败"); 
        }
        else{
            socklen_t len = sizeof(adin);
            do{
            memset(&adin,0,sizeof(adin));
            memset(recstr,0,1024);
            if(recvfrom(socs, recstr, 1024, 0, (struct sockaddr*)&adin, &len)<0){
                cout<<"接收错误。"<<endl;
            }
            recvs.clear();
            recvs=recstr;
            if(recvs.substr(0,7)==reqw){
                now->cliad=adin;
                now->flag=0;
                now->next=new cliaddr;
                now=now->next;
                now->next=NULL;
                if(sendto(socs, "receive please waiting", 23, 0, (struct sockaddr*)&adin, len)<0){
                cout<<"返回错误。"<<endl;
                }
                else{
                    cout<<inet_ntoa(adin.sin_addr)<<':'<<ntohs(adin.sin_port)<<"加入等待"<<endl;
                    logfile<<inet_ntoa(adin.sin_addr)<<':'<<ntohs(adin.sin_port)<<"加入等待"<<endl;
                }
            }
            else if(recvs.substr(0,7)==reql){
                addrins=(char *)(recvs.c_str()+8);
                adt=inet_addr(addrins);
                now1=head;
                cout<<inet_ntoa(adin.sin_addr)<<':'<<ntohs(adin.sin_port)<<"请求连接"<<addrins<<endl;
                cout<<recvs<<endl;
                
                logfile<<inet_ntoa(adin.sin_addr)<<':'<<ntohs(adin.sin_port)<<"请求连接"<<addrins<<endl;
                do{
                    if(now1->cliad.sin_addr.s_addr==0){break;}
                    if((now1->cliad.sin_addr.s_addr)==adt){
                        if(now1->flag==0){break;}
                        else{now1=now1->next;}
                    }
                    else{now1=now1->next;}
                }while(now1!=NULL);
                if((now1->cliad.sin_addr.s_addr)==0||now1==NULL){
                    if(sendto(socs, "no waiting find", 16, 0, (struct sockaddr*)&adin, len)<0){
                        cout<<"返回错误。"<<endl;
                    }
                    else{
                        cout<<"未找到等待设备"<<endl;
                        logfile<<"未找到等待设备"<<endl;
                    }
                }
                else{
                    addrins=(char *)(&(now1->cliad));
                    if(sendto(socs, addrins, sizeof(now1->cliad), 0, (struct sockaddr*)&adin, len)<0){
                        cout<<"返回错误。"<<endl;
                    }
                    addrins=(char *)(&adin);
                    if(sendto(socs, addrins, sizeof(adin), 0, (struct sockaddr*)&(now1->cliad), len)<0){
                        cout<<"返回错误。"<<endl;
                    }
                    cout<<"已链接"<<endl;
                    logfile<<"已链接"<<endl;
                    now1->flag=1;
                }

            }
            else if(recvs.substr(0,7)==reqa){
                now1=head;
                do{
                    if(now1->cliad.sin_addr.s_addr==0){break;}
                    if((now1->cliad.sin_addr.s_addr)==adin.sin_addr.s_addr){
                        now1->flag=0;break;
                    }
                    else{now1=now1->next;}
                }while(now1!=NULL);
                if(sendto(socs, "prepareing", 11, 0, (struct sockaddr*)&adin, len)<0){
                        cout<<"返回错误。"<<endl;
                }
                else{
                    cout<<inet_ntoa(adin.sin_addr)<<':'<<ntohs(adin.sin_port)<<"丢失链接"<<endl;
                    logfile<<inet_ntoa(adin.sin_addr)<<':'<<ntohs(adin.sin_port)<<"丢失链接"<<endl;
                }

            }
            else if(recvs.substr(0,7)==reqc){
                now1=head;
                if((now1->cliad.sin_addr.s_addr)==adin.sin_addr.s_addr){
                    head=head->next;
                    delete now1;
                }
                else{
                    do{
                        if(now1->cliad.sin_addr.s_addr==0){break;}
                        if((now1->next->cliad.sin_addr.s_addr)==adin.sin_addr.s_addr){
                            now2=now1->next;
                            now1->next=now2->next;
                            delete now2;
                            break;
                        }
                        else{now1=now1->next;}
                    }while(now1->next!=NULL);
                }
                if(sendto(socs, "canceled", 9, 0, (struct sockaddr*)&adin, len)<0){
                        cout<<"返回错误。"<<endl;
                    }
                else{
                    cout<<inet_ntoa(adin.sin_addr)<<':'<<ntohs(adin.sin_port)<<"取消链接"<<endl;
                    logfile<<inet_ntoa(adin.sin_addr)<<':'<<ntohs(adin.sin_port)<<"取消链接"<<endl;
                    }
            }
            
            usleep(10000);
            }while(1);

        }

    }
    close(socs);
    now=head;
    do{
        now=head->next;
        delete head;
        head=now;
    }while(head!=NULL);
    return 0;
}